/*
 ============================================================================
 Name		: HttpEngineTimer.cpp
 Author	  : wangchao
 Version	 : 1.0
 Copyright   :
 Description : CHttpEngineTimer implementation
 ============================================================================
 */

#include "HttpEngineTimer.h"

CHttpEngineTimer::CHttpEngineTimer( ) :
    CActive( EPriorityStandard ) // Standard priority
{
}

CHttpEngineTimer* CHttpEngineTimer::NewLC( HttpEngineTimerObserver* observer )
{
    CHttpEngineTimer* self = new ( ELeave ) CHttpEngineTimer( );
    CleanupStack::PushL( self );
    self->ConstructL( observer );
    return self;
}

CHttpEngineTimer* CHttpEngineTimer::NewL( HttpEngineTimerObserver* observer )
{
    CHttpEngineTimer* self = CHttpEngineTimer::NewLC( observer );
    CleanupStack::Pop( ); // self;
    return self;
}

void CHttpEngineTimer::ConstructL( HttpEngineTimerObserver* observer )
{
    User::LeaveIfError( iTimer.CreateLocal( ) ); // Initialize timer
    CActiveScheduler::Add( this ); // Add to scheduler
    _observer = observer;
}

CHttpEngineTimer::~CHttpEngineTimer( )
{
    Cancel( ); // Cancel any request, if outstanding
    iTimer.Close( ); // Destroy the RTimer object
    // Delete instance variables if any
}

void CHttpEngineTimer::DoCancel( )
{
    iTimer.Cancel( );
}

void CHttpEngineTimer::StartL( int aDelay )
{
    Cancel( ); // Cancel any request, just to be sure
    _delay = aDelay * 1000;

    iTimer.After( iStatus, _delay ); // Set for later
    SetActive( ); // Tell scheduler a request is active
}

void CHttpEngineTimer::RunL( )
{
    bool canceled = true;
    // Do something
    if( _observer )
    {
        _observer ->TimeOut( this, canceled );
    }
    if( !canceled )
    {
        iTimer.After( iStatus, _delay ); // Set for 1 sec later
        SetActive( ); // Tell scheduler a request is active
    }
}

TInt CHttpEngineTimer::RunError( TInt aError )
{
    return aError;
}
